# -*- coding: utf-8 -*-
#some player handeling perhaps?

import pygame

import data
import weapon
import default

class Falling:
    pass

#this should probably be the base-class for the player class, but right now... well never mind
class Character(pygame.sprite.Sprite):
    def __init__(self, image, pos, vel=(0,0), state=Falling()):
        pygame.sprite.Sprite.__init__(self)

        # An animation is a strip of images, each having
        # its place in the image matrix. Yes matrices ARE
        # what we want here! example: self.animation.falling, shooting.
        # These states are defined by the state machine of the character
        #self.animation, self.rect = animation.load(image, -1)
        #self.surface = self.animation.get(state)

        self.image, self.rect = data.load_image(image, -1)

        #setup physics
        self.rect.move(pos)
        self.vel = vel

        self.max_vel = 100
        
    def update(self, acc=0, t=1):
        """Update position according to speed, and check for collisions"""


        if abs(self.vel[0]) > default.friction:
            #We have no mass...
            self.accelerate((-default.friction*abs(self.vel[0])/self.vel[0], 0))# + default.g)
        else:
            self.vel = (0, self.vel[1])
            
        self.rect.move_ip(t*self.vel[0], t*self.vel[1])  

    def accelerate(self, acc=0, t=1):
        """Accelerate according to vector acc for t ticks"""

        self.vel = (self.vel[0] + t*acc[0], self.vel[1] + t*acc[1])

    #def face(direction):
        
class Player(Character):
    """A character that can handle weapons, has life, state,
    and functions that the user may request"""
    def __init__(self, image, pos, vel=(0,0), hp=100,
                 weapons=[weapon.Chainsaw()], state=Falling()):
        """image: image data file to load
        pos: position in world
        vel: starting velocity
        hp: character life
        weapons: list of classes of all the weapons that player carries"""
        Character.__init__(self, image, pos, vel, state)

        self.hp = hp
        self.weapons = weapons

        self.weapon = self.weapons[0]
        #self.weapon = self.next_weapon()

        self.state = state

        self.next_state = None

    def update(self, acc=0, t=1):
        """Update player"""
        Character.update(self, acc, t)

        if self.next_state:
            state.change(next_state)

        #A weapnon has a state too
        #self.weapon.update(t)
    def change_weapon(self, index):
        """Change to weapon indicated by index, or return false if
        no such weapon in inventory."""
        if weapons[index]:
            self.weapon = weapons[index]
            return True
        else:
            return False
        
    def aim(self, angle):
        """Aim in the angle (radians), where 0 is right ahead"""
        self.weapon.aim(angle)
        
    def shoot(self):
        """Fire weapon"""
        self.weapon.fire()


    ## Requests from user
    def jetpack(self, key_press):
        """Use jetpack"""
        pass
    def left(self, key_press):
        """Accelerate player to the left"""
        if key_press:
            # So far you can only push him sideways by pressing the button
            #(BUT IF YOU DO IT FAST ENOUGH, YOU CAN MAKE HIM RUN LIKE THE WIND!)
            self.accelerate((-6, 0))
        
    def right(self, key_press):
        """Accelerate player to the right"""
        if key_press:
            # No magic
            self.accelerate((6, 0))
        
